package org.jeecg.modules.demo.ipds.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.regexp.RE;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ipds.entity.MyIpWrod;
import org.jeecg.modules.demo.ipds.service.IMyIpWrodService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 我的协议文档
 * @Author: jeecg-boot
 * @Date:   2024-01-29
 * @Version: V1.0
 */
@Api(tags="我的协议文档")
@RestController
@RequestMapping("/ipds/myIpWrod")
@Slf4j
public class MyIpWrodController extends JeecgController<MyIpWrod, IMyIpWrodService> {


	@Autowired
	private IMyIpWrodService myIpWrodService;

	/**
	 * 分页列表查询
	 *
	 * @param myIpWrod
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "我的协议文档-分页列表查询")
	@ApiOperation(value="我的协议文档-分页列表查询", notes="我的协议文档-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="ipds/MyIpWrodList")
	public Result<IPage<MyIpWrod>> queryPageList(MyIpWrod myIpWrod,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MyIpWrod> queryWrapper = QueryGenerator.initQueryWrapper(myIpWrod, req.getParameterMap());
		Page<MyIpWrod> page = new Page<MyIpWrod>(pageNo, pageSize);
		IPage<MyIpWrod> pageList = myIpWrodService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 @ApiOperation(value="所有协议文档-分页列表查询", notes="所有协议文档-分页列表查询")
	 @GetMapping(value = "/listAll")
	 public Result<IPage<MyIpWrod>> queryPageListMy(MyIpWrod myIpWrod,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
		 QueryWrapper<MyIpWrod> queryWrapper = QueryGenerator.initQueryWrapper(myIpWrod, req.getParameterMap());
		 Page<MyIpWrod> page = new Page<MyIpWrod>(pageNo, pageSize);
		 IPage<MyIpWrod> pageList = myIpWrodService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }



	 /**
	  *  编辑
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "我的协议文档-设置/取消模板")
	 @ApiOperation(value="我的协议文档-设置/取消模板", notes="我的协议文档-设置/取消模板")
	 @RequestMapping(value = "/setTemplate", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody List<String> id) {
		 List<MyIpWrod> myIpWrods = myIpWrodService.listByIds(id);

		 for (MyIpWrod byId : myIpWrods) {

		 if ( byId.getIsMode().equals("false")){
			 UpdateWrapper updateWrapper = new UpdateWrapper();
			 updateWrapper.eq("id",byId.getId());
			 updateWrapper.set("is_mode","true");
			 myIpWrodService.update(updateWrapper);

		 }

		 if (byId.getIsMode().equals("true")){
			 UpdateWrapper updateWrapper = new UpdateWrapper();
			 updateWrapper.eq("id",byId.getId());
			 updateWrapper.set("is_mode","false");
			 myIpWrodService.update(updateWrapper);

		 }

		 }


		 return Result.OK("成功!");

	 }





	 @ApiOperation(value="我的协议文档-模板查询", notes="我的协议文档-模板查询")
	 @GetMapping(value = "/listTemplate")
	 public Result<?> queryMode(@RequestParam(name = "name",required = false) String name) {
		 QueryWrapper<MyIpWrod> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("is_mode","true");
		if (name!=null&& name.length()>0){
			queryWrapper.like("name",name);
		}
		 List<MyIpWrod> list = myIpWrodService.list(queryWrapper);

		 return Result.OK(list);
	 }

	 @ApiOperation(value="我的协议文档-模板分页查询", notes="我的协议文档-模板分页查询")
	 @GetMapping(value = "/listTemplatePage")
	 public Result<?> queryMode(@RequestParam(name = "name",required = false) String name ,
								@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 QueryWrapper<MyIpWrod> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("is_mode","true");
		 if (name!=null&& name.length()>0){
			 queryWrapper.like("name",name);
		 }
		 Page<MyIpWrod> page = new Page<MyIpWrod>(pageNo, pageSize);
		 IPage<MyIpWrod> list = myIpWrodService.page(page,queryWrapper);
		 return Result.OK(list);
	 }



	 @ApiOperation(value="我的协议文档-使用模板创建", notes="我的协议文档-使用模板创建")
	 @PutMapping(value = "/templateAdd")
	 public Result<?> templateAdd(@RequestBody JSONObject jsonObject) {
		 Object id = jsonObject.get("id");
		 myIpWrodService.templateAdd(id.toString());

		 return Result.OK("创建成功");
	 }


	
	/**
	 *   添加
	 *
	 * @param myIpWrod
	 * @return
	 */
	@AutoLog(value = "我的协议文档-添加")
	@ApiOperation(value="我的协议文档-添加", notes="我的协议文档-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MyIpWrod myIpWrod) {
		myIpWrodService.save(myIpWrod);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param myIpWrod
	 * @return
	 */
	@AutoLog(value = "我的协议文档-编辑")
	@ApiOperation(value="我的协议文档-编辑", notes="我的协议文档-编辑")

	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MyIpWrod myIpWrod) {
		myIpWrodService.updateById(myIpWrod);
		return Result.OK("编辑成功!");
	}


	 /**
	  *  编辑
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "我的协议文档-放入/恢复-回收站")
	 @ApiOperation(value="我的协议文档-放入/恢复-回收站", notes="我的协议文档-编辑")
	 @RequestMapping(value = "/frhsz/{deleted}", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody List<String> ids,@PathVariable(value = "deleted") String deleted) {
//		 System.out.println(deleted);
//		myIpWrodService.updateBatchByIDsetDeleted(ids);
		 UpdateWrapper<MyIpWrod> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.in("id",ids);
		 updateWrapper.set("deleted",deleted);
		 myIpWrodService.update(null,updateWrapper);
		 return Result.OK("编辑成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "我的协议文档-通过id删除")
	@ApiOperation(value="我的协议文档-通过id删除", notes="我的协议文档-通过id删除")

	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
//		myIpWrodService.removeById(id);
		myIpWrodService.removeAndChild(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "我的协议文档-批量删除")
	@ApiOperation(value="我的协议文档-批量删除", notes="我的协议文档-批量删除")

	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.myIpWrodService.removeByIds(Arrays.asList(ids.split(",")));
		List<String> list = Arrays.asList(ids.split(","));
		for (String id : list) {
			myIpWrodService.removeAndChild(id);
		}


		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "我的协议文档-通过id查询")
	@ApiOperation(value="我的协议文档-通过id查询", notes="我的协议文档-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<MyIpWrod> queryById(@RequestParam(name="id",required=true) String id) {
		MyIpWrod myIpWrod = myIpWrodService.getById(id);
		if(myIpWrod==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(myIpWrod);
	}






    /**
    * 导出excel
    *
    * @param request
    * @param myIpWrod
    */

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MyIpWrod myIpWrod) {
        return super.exportXls(request, myIpWrod, MyIpWrod.class, "我的协议文档");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MyIpWrod.class);
    }

	@ApiOperation(value="我的协议文档-通过id导出文档", notes="我的协议文档-通过id查询")
	@RequestMapping(value = "/exportWord", method = RequestMethod.GET)
	public Result<?> exportWord(HttpServletRequest request, HttpServletResponse response,
									 @RequestParam(value = "id") Long id) {
		Result<String> result = new Result();
		try {
			String path = myIpWrodService.exportWord(request, response, id);
			result.setResult(path);
			result.setMessage("导出成功");
			result.setSuccess(true);
			result.setCode(CommonConstant.SC_OK_200);
		} catch (JeecgBootException e) {
			log.error(e.getMessage());
			result.setSuccess(false);
			result.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
			result.setMessage("导出失败");
		}
		return result;
	}

}
